import sys
from PyQt5 import QtCore,QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow
from newModel import Ui_MainWindow2
# 导入模块
import testMysql
#mysql和qt5图形联动Demo，数据库查询，更新和删除
class MainWindow(QMainWindow,Ui_MainWindow2):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        #loadUi('test.ui', self)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.say)
        self.pushButton_2.clicked.connect(self.edit_cell)
        self.showData()
        
    def showData(self):

        # 准备数据模型
        self.sm=QtGui.QStandardItemModel()
         
        #设置数据头栏名称
        self.sm.setHorizontalHeaderItem(0, QtGui.QStandardItem("编号"))
        self.sm.setHorizontalHeaderItem(1, QtGui.QStandardItem("姓名"))
        self.sm.setHorizontalHeaderItem(2, QtGui.QStandardItem("生日")) 
        #设置数据条目
        myList=testMysql.mysqlData()
        i=0
        for row in myList: 
            print(row['cu'],",",row['fn'],",",row['brd'])
            self.sm.setItem(i, 0, QtGui.QStandardItem(row['cu']))
            self.sm.setItem(i, 1, QtGui.QStandardItem(row['fn']))
            self.sm.setItem(i, 2, QtGui.QStandardItem(row['brd']))
            i=i+1
    
        #设置条目颜色和字体
        self.sm.item(0, 0).setForeground(QtGui.QBrush(QtGui.QColor(255, 0, 0)))     
        self.sm.item(0, 0).setFont(QtGui.QFont("Times", 10, QtGui.QFont.Black))
         
        self.sm.item(3, 1).setBackground(QtGui.QBrush(QtGui.QColor(255, 255, 0)))
         
        #按照编号排序
        #self.sm.sort(1, QtCore.Qt.DescendingOrder)
         
        #将数据模型绑定到QTableView
        self.tableView.setModel(self.sm)
 
        #QTableView
        self.tableView.setColumnWidth(0,100)
        self.tableView.setColumnWidth(1,200)
    
    def showData2(self):

        # 准备数据模型
        #self.sm=QtGui.QStandardItemModel()
        
        try:            
            #清除全部数据显示
            self.sm.clear() 
            # #Todo 优化2 添加数据
            #qItem1=QtGui.QStandardItem('row %s,column %s' % (11,11))
            #qItem2=QtGui.QStandardItem('row %s,column %s' % (11,11))
            #qItem3=QtGui.QStandardItem('row %s,column %s' % (11,11))
            #self.sm.appendRow([qItem1,qItem2 ,qItem3])
            #设置数据头栏名称
            self.sm.setHorizontalHeaderItem(0, QtGui.QStandardItem("编号2"))
            self.sm.setHorizontalHeaderItem(1, QtGui.QStandardItem("姓名2"))
            self.sm.setHorizontalHeaderItem(2, QtGui.QStandardItem("生日2")) 
            #设置数据条目
            myList=testMysql.mysqlData()
            i=0
            for row in myList: 
                print(row['cu'],",",row['fn'],",",row['brd'])
                self.sm.setItem(i, 0, QtGui.QStandardItem(row['cu']))
                self.sm.setItem(i, 1, QtGui.QStandardItem(row['fn']))
                self.sm.setItem(i, 2, QtGui.QStandardItem(row['brd']))
                i=i+1            
        except Exception as e:
            print(e)
        
    def say(self):      
        try:  
            self.label.setText("哈哈哈")
            print("哈哈哈")    
            self.showData2()
        except Exception as e:
            print(e)
    def edit_cell(self):  
        try:                          # 编辑数据调用的方法
            r = self.tableView.currentIndex().row()            # 获取行号
            c = self.tableView.currentIndex().column()         # 获取列序
            print(r,",",c)
            curr_data = self.tableView.currentIndex().data()
            print(curr_data)
            item = self.sm.index(r,c)
            print(item)
            print(item.data())
        #save_cell(obj,int(item.data()),param)
        except Exception as e:
            print(e)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
